package com.xll.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xll.entity.Food;
import com.xll.entity.Hotel;
import com.xll.mapper.HotelMapper;
import com.xll.service.HotelService;
import com.xll.utils.PageResult;
import com.xll.utils.QueryInfo;
import com.xll.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class HotelServiceImpl implements HotelService {

    @Autowired
    private HotelMapper hotelMapper;
    @Override
    public Result delete(Long id) {
        hotelMapper.delete(id);
        return Result.success("酒店信息删除成功");
    }

    @Override
    public Result update(Hotel hotel) {
        hotelMapper.update(hotel);
        return Result.success("酒店信息更新成功");
    }

    @Override
    public Result findPage(QueryInfo queryInfo) {
        PageHelper.startPage(queryInfo.getPageNumber(), queryInfo.getPageSize());
        Page<Hotel> page = hotelMapper.findPage(queryInfo.getQueryString());
        return Result.success("酒店信息查询成功", new PageResult(page.getTotal(), page.getResult()));
    }

    @Override
    public Result insert(Hotel hotel) {
        hotelMapper.insert(hotel);
        return  Result.success("酒店信息插入成功");
    }

    @Override
    public Result findAllByType(JSONObject jsonObject) {
        PageHelper.startPage(jsonObject.getInteger("pageNumber"), jsonObject.getInteger("pageSize"));
        Long typeId = jsonObject.getLong("typeId");
        String keywords = jsonObject.getString("keywords");
        Page<Food> menus = hotelMapper.findMenus(keywords);
        return PageResult.pageResult(menus.getTotal(), menus.getResult());
    }

    @Override
    public Result findById(Long id) {
        if(id==null){
            return  Result.fail("请传递酒店编号");
        }
        Hotel hotel=hotelMapper.findById(id);
        return Result.success("酒店信息查询成功",hotel);
    }
}
